<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-11-26
 * Time: 9:45
 */

namespace app\common\model;



use think\Db;
use think\Model;

class Messagem extends Model
{
    /**
     *消息模块列表
     */
    public static function messageList($map = [], $page)
    {
        return  Db::name('message_module')->where($map)->order('mm_id DESC')->paginate(15, false, ['page' => $page]);
    }

    /**
     * 添加消息模块
     */
    public static function messageSave($data)
    {
        $data['mm_create_time'] = time();
        $data['mm_update_time'] = time();
        return Db::name('message_module')->insert($data);
    }

    /**
     * 消息模块详情
     */
    public static function detail($id)
    {
        return Db::name('message_module')->where('mm_id', $id)->find();
    }

    /**
     * 修改模块详情
     */
    public static function updateType($data)
    {
        return Db::name('message_module')->update($data);
    }

    /**
     * 禁用启用
     */
    public static function editStatus($id, $status)
    {
        return Db::name('message_module')->where('mm_id', $id)->update(['mm_status' => $status, 'mm_update_time' => time()]);
    }


    /**
     * 用户消息列表
     */
    public static function userMessageList($map = [], $page)
    {
        return Db::name('message')->alias('m')
            ->join('user', 'm_uid = u_id', 'left')
            ->where($map)
            ->field('m.*, u_phone, u_email, u_nickname')
            ->order('m_id DESC')
            ->paginate(15,false, ['page' => $page]);
    }

    /**
     * 用户消息的详情
     */
    public static function userMessageInfo($id)
    {
        return Db::name('message')->alias('m')
            ->join('user', 'm_uid = u_id', 'left')
            ->where('m_id', $id)
            ->field('m.*, u_phone, u_email, u_nickname')
            ->order('m_uid DESC')
            ->find();
    }

    /**
     * 添加用户消息
     */
    public static function addUserMessage($uid, $title, $content, $type)
    {
        return Db::name('message')->insertGetId([
            'm_uid'         => $uid,
            'm_title'       => $title,
            'm_content'     => $content,
            'm_type'        => $type,
            'm_status'      => 1,
            'm_create_time' => time(),
        ]);
    }

    /**
     * 前端用户消息列表展示
     */
    public static function getListByUid($uid, $page)
    {
        return Db::name('message')
            ->field('m_id, m_uid, m_title, m_content, from_unixtime(m_create_time) m_create_time')
            ->where('m_uid', $uid)
            ->order('m_id DESC')
            ->paginate(15,false, ['page' => $page]);
    }
    /**
     * 前端用户删除消息
     */
    public static function delMesById($data)
    {
        return Db::name('message')
            ->where('m_id', $data['m_id'])
            ->where('m_uid', $data['m_uid'])
            ->delete();
    }

    /**
     * 查询用户有没有领取当天签到积分
     */
    public static function getUserDayPoints($uid)
    {
        //当天开始时间
        $startTime = strtotime(date("Y-m-d",time()));
        //当天结束时间
        $endTime = $startTime+60*60*24;
        $mid = Db::name('message')
            ->where('m_uid', $uid)
            ->where('m_type', 1)
            ->where('m_create_time', '>', $startTime)
            ->where('m_create_time', '<', $endTime)
            ->value('m_id');
        $data['s_check_score'] = Db::name('systems')->where('s_id', 1)->value('s_check_score');
        if ($mid) {
            $data['type'] = 1;//已签到
        }else {
            $data['type'] = 2;//未签到
        }
        return $data;
    }
    /**
     * 用户签到
     */
    public static function userReceive($uid)
    {
        $s_check_score = Db::name('systems')->where('s_id', 1)->value('s_check_score');
        Db::startTrans();
        try{
            Db::name('user')->where('u_id', $uid)->setInc('u_integral',$s_check_score);
            $title = '每日签到';
            $content = '每日签到。获得奖励'.$s_check_score.'积分。';
            $mid = self::addUserMessage($uid, $title, $content,1);
            Db::commit();
            return $mid;
        } catch(\Exception $e) {
            Db::rollback();
            return false;
        }

    }

    /**
     * 用户的积分列表
     */
    public static function PointsList($uid, $page)
    {
        return Db::name('message')
            ->field('m_title, m_content, from_unixtime(m_create_time) m_create_time')
            ->where('m_uid', $uid)
            ->where('m_type', '<', 4)
            ->order('m_id DESC')
            ->paginate(15,false, ['page' => $page]);
    }

    /**
     * 用户购买会员的记录列表
     */
    public static function openMemberList($uid, $page)
    {
        return Db::name('message')
            ->field('m_title, m_content, from_unixtime(m_create_time) m_create_time')
            ->where('m_uid', $uid)
            ->where('m_type', 5)
            ->order('m_id DESC')
            ->paginate(15,false, ['page' => $page]);
    }
}